package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import bean.Admin;

public class AdminDAO extends DB {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	

		public int addAdmin(Admin Admin)
		{
			try
			{
				conn = this.getConn();
				
				String sql = "insert into Admin(username,password,kind) " +
						"values(?,?,?) ";
				
				pstmt = conn.prepareStatement(sql);
				
				pstmt.setString(1, Admin.getUsername());
				pstmt.setString(2, Admin.getPassword());
				pstmt.setInt(3, Admin.getKind());
		           
				return pstmt.executeUpdate();
				
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			}finally{
				this.closeAll(conn, pstmt, rs);
			}
		}
	

	public Admin checkAdmin(String username ,String password)
	{
		
		Admin admin = null;							
		try
		{
			conn = this.getConn();					
			String sql  = "select * from admin where username =? and password = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, password);
		    rs =  pstmt.executeQuery();				
			while (rs.next())
			{
				admin= new Admin();				
				admin.setId(rs.getInt("id"));
				admin.setUsername(rs.getString("username"));
				admin.setKind(rs.getInt("kind"));
			}
			return admin;		
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;	
		}finally
		{
			this.closeAll(conn, pstmt, rs);	
		}
	}
	

		public Admin QueryAdminByID(int id)
		{
			
			Admin admin = null;							
			try
			{
				conn = this.getConn();					
				String sql  = "select * from admin where id= '"+ id +"'";
				pstmt = conn.prepareStatement(sql);
			    rs =  pstmt.executeQuery();				
				while (rs.next())
				{
					admin= new Admin();				
					admin.setId(rs.getInt("id"));
					admin.setUsername(rs.getString("username"));
					admin.setPassword(rs.getString("password"));
					admin.setKind(rs.getInt("kind"));
				}
				return admin;		
			} catch (Exception e)
			{
				e.printStackTrace();
				return null;	
			}finally
			{
				this.closeAll(conn, pstmt, rs);	
			}
		}
	

		public int UpdataAdmin(Admin admin,int id)
		{
			try
			{
				conn = this.getConn();
				
				String sql = "update admin set username=?,password=? ,kind=? where id=?";
				
				pstmt = conn.prepareStatement(sql);
				
				pstmt.setString(1, admin.getUsername());
				pstmt.setString(2, admin.getPassword());
				pstmt.setInt(3, 1);
				pstmt.setInt(4, id);

		           
				return pstmt.executeUpdate();
				
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			}finally{
				this.closeAll(conn, pstmt, rs);
			}
		}
		

	public int chagePassword(Admin admin,int id)
	{
		try
		{
			conn = this.getConn();
			
			String sql = "update admin set password=? where id='"+ id +"'";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, admin.getPassword());
   
			return pstmt.executeUpdate();
			
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
	}
	

	public List<Admin> QueryAdminList()
	{
		
		List<Admin> list = new ArrayList<Admin>();	
		Admin admin = null;							
		try
		{
			conn = this.getConn();					
			String sql = "select * from admin where kind='1' ";
			pstmt = conn.prepareStatement(sql);		
		    rs =  pstmt.executeQuery();				
			while (rs.next())
			{
				admin= new Admin();					

				admin.setId(rs.getInt("id"));
				admin.setUsername(rs.getString("username"));
				admin.setPassword(rs.getString("password"));

				
				list.add(admin);
			}
			return list;		
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;	
		}finally
		{
			this.closeAll(conn, pstmt, rs);	
		}
	}
	
	

	public int deleteAdminById(int id)
	{
		int i = 0;
		try
		{
			conn = this.getConn();
			String sql = "delete from admin where id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);

			i = pstmt.executeUpdate();
			this.closeAll(conn, pstmt, rs);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return i;
	}
		

}
